package com.gxa.sso.service.impl;

import com.gxa.sso.exception.CustomerException;
import com.gxa.sso.pojo.User;
import com.gxa.sso.repository.UserRepository;
import com.gxa.sso.result.MessageCode;
import com.gxa.sso.routing.DataSource;
import com.gxa.sso.service.UserService;
import com.gxa.sso.utils.JedisTemplete;
import com.gxa.sso.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by zxd on 2020/12/16
 **/
@Service("userService")
public class UserServiceImpl implements UserService {


    @Autowired
    private JedisTemplete jedisTemplete;

    @Autowired
    private UserRepository userRepository;

    @Autowired(required = false)
    private HttpServletRequest request;

    public static final String LOGIN="sfvasfewsjkfbacvasjcasjcj";

    @Override
    public Map<String, String> login(String phone, String code) throws CustomerException {

        //一键登录

        //1.获取服务器存储的验证码
        String serverCode = jedisTemplete.get(SmsServiceImpl.LOGIN_SMS + phone);
        if(StringUtils.isEmpty(serverCode)){
            //验证码失效
            throw  new CustomerException(MessageCode.LOGIN_SMS_EXPIRE);
        }
        if(!code.equals(serverCode)){
            //验证码输入错误
            throw  new CustomerException(MessageCode.LOGIN_SMS_ERROR);
        }

        User user = userRepository.findUserByPhone(phone);
        if(user==null){
            //匿名注册
            System.out.println("用户没有账号，匿名注册:"+phone);
            user=new User();
            user.setPhone(phone);
            user.setUsername("用户:"+phone);
            user.setStatus(1);//正常账号

            userRepository.save(user);

        }

        if(user.getStatus()!=1){
            throw new CustomerException(MessageCode.USER_NO);
        }

        //我怎么知道他是手机端?

        //获取上次的token,获取后移除上一次的登录信息
        if(!StringUtils.isEmpty(user.getToken())){
            jedisTemplete.remove(LOGIN+user.getToken());
        }

        //生成token，存储登录信息
        String token=UUID.randomUUID().toString().replace("-","");

        //处理登录

        // 设置ip
        user.setIpAddress(request.getRemoteAddr());
        user.setLastLoginTime(new Date());
        user.setToken(token);
        userRepository.save(user);//更新ip和登录时间


        jedisTemplete.set(LOGIN+token,JsonUtils.objectToJson(user));
        jedisTemplete.expire(LOGIN+token,60*60*2);

        Map<String,String> result=new HashMap<>();
        result.put("token",token);

        return result;
    }

    @Override
    public User queryUserByToken(String token) throws CustomerException {

        String result = jedisTemplete.get(LOGIN + token);
        if (StringUtils.isEmpty(result)) {
            System.out.println("无效token");
            throw new CustomerException(MessageCode.TOKEN_NO);
        }

        //要更新失效时间
        jedisTemplete.expire(LOGIN+token,60*60*2);

        return JsonUtils.jsonToPojo(result,User.class);
    }
}
